
#include "S_10_Enhance_doSet.h"
#include "string.h"
#include "mi_iid_define.h"
#include "spec.h"
#include "main.h"
#include "section1_set_func.h"
#include "section_define.h"


static void P_10_16_RoomSize_doSet(property_operation_t *o)
{
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_NUMBER)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

    // 简单判断数据取值范围是否合法，如果不合法，返回代码: OPERATION_ERROR_VALUE
    if (o->value.data.number.value.integerValue < 0 || o->value.data.number.value.integerValue > 6)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

    // TODO: 执行写操作: o->value.data.number.value.integerValue;

	LOG_INFO("%s, %ld\r\n",__func__,o->value.data.number.value.integerValue);
	
    // 如果成功，返回代码: OPERATION_OK
    o->code = OPERATION_OK;
   
	A1F_10_16_Set(o->value.data.number.value.integerValue);		
//	BeepStart();	
    //上报状态，通知app状态变化，统一APP修改、定时器触发后的上报机制
//    P_10_16_RoomSize_doChange_notify(o->value.data.number.value.integerValue);

    return;
}

static void P_10_1_FanPercent_doSet(property_operation_t *o)
{
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_NUMBER)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

    // 判断数据取值范围是否合法，如果不合法，返回代码: OPERATION_ERROR_VALUE
    if (o->value.data.number.value.integerValue < 0 || o->value.data.number.value.integerValue > 101)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }
	
	if(AirGetSwitchState() == SWITCH_OFF) 
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}

	if(GetAirMode() == DEHUMIDIFYMODE)
	{
		o->code = OPERATION_ERROR_VALUE;
		return;
	}
	
	if(AirGetSelfCleanModeSwitchState() == SWITCH_ON)
	{
		o->code = OPERATION_ERROR_VALUE;
        return;
	}
	
    // TODO: 执行写操作: o->value.data.number.value.integerValue;

	LOG_INFO("%s, %ld\r\n",__func__,o->value.data.number.value.integerValue);
	
    // 如果成功，返回代码: OPERATION_OK
    o->code = OPERATION_OK;
    
	A1B_10_1_Set(o->value.data.number.value.integerValue);
//	BeepStart();		
    //上报状态，通知app状态变化，统一APP修改、定时器触发后的上报机制
//    P_10_1_FanPercent_doChange_notify(o->value.data.number.value.integerValue);

    return;
}

static void P_10_18_MaxfanSelect_doSet(property_operation_t *o)
{
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_BOOLEAN)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

    // TODO: 执行写操作: o->value.data.boolean.value;

	LOG_INFO("%s, %d\r\n",__func__,o->value.data.boolean.value);
	
    // 如果成功，返回代码: OPERATION_OK
    o->code = OPERATION_OK;
	A03_10_18_Set(o->value.data.boolean.value);
//	BeepStart();
    //上报状态，通知app状态变化，统一APP修改、定时器触发后的上报机制
//    P_10_18_MaxfanSelect_doChange_notify(o->value.data.boolean.value);

    return;
}

static __IO float g_AppSetRealTime = 0.0;
static void P_10_3_Timer_doSet(property_operation_t *o)
{
	char *Temp = NULL;
	char *Buf[5] = {0};
	uint8_t Data1[2] = {0};
	uint16_t Data2[2] = {0};
	uint8_t Cnt = 0;

    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_STRING)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

    // TODO: 执行写操作: o->value.data.string.value;
		
	LOG_INFO("%s, %s\r\n",__func__,o->value.data.string.value);
	
		PRINTF("%s %s\r\n",__FUNCTION__,o->value.data.string.value);
    // 如果成功，返回代码: OPERATION_OK
    o->code = OPERATION_OK;
    
    //上报状态，通知app状态变化，统一APP修改、定时器触发后的上报机制
//    P_10_3_Timer_doChange_notify(o->value.data.string.value);
	if((GetEnterSelfTestModeFlag() == false) && (GetEnterAgingTestModeFlag() == false))
	{				
		Temp = strtok(o->value.data.string.value,",");
		while((Temp != NULL) && (Cnt < 4)) 
		{
			Buf[Cnt] = Temp;
			Cnt ++;
			Temp = strtok(NULL, ",");
		}		
		Data1[0] = atoi(Buf[0]);//开关倒计时
		Data2[0] = atoi(Buf[1]);//定时时间
		Data1[1] = atoi(Buf[2]);//开/关机指令
		Data2[1] = atoi(Buf[3]);//剩余时间
		if((1 == Data1[0]) && (Data1[1]>0) && (Data2[0]>0))
		{	
			if(1 == Data1[1])//定时开机
			{
				g_tTimingOnInfo.HourTime = Data2[0]/60;
				g_tTimingOnInfo.MinuteTime = Data2[0]%60;
				g_tTimingOnInfo.RealTime = (float)((Data2[0]/60)*1.0) + ((float)(Data2[0]%60)/60.0);
				g_AppSetRealTime = (float)((Data2[0]/60)*1.0) + ((float)(Data2[0]%60)/60.0);
//					PRINTF("%d %d %.1f %.1f\r\n",g_tTimingOnInfo.HourTime,g_tTimingOnInfo.MinuteTime,g_tTimingOnInfo.RealTime,g_tTimingOnInfo.MemoryTime);
				DisplayTimingNum(g_AppSetRealTime);
			}
			else if(2 == Data1[1])//定时关机
			{
//			PRINTF("%d %d %d %d\r\n",Data1[0],Data2[0],Data1[1],Data2[1]);
				g_tTimingOffInfo.HourTime = Data2[0]/60;
				g_tTimingOffInfo.MinuteTime =  Data2[0]%60;
				g_tTimingOffInfo.RealTime = (float)((Data2[0]/60)*1.0) + ((float)(Data2[0]%60)/60.0);
				g_AppSetRealTime = g_tTimingOffInfo.RealTime;			
//					PRINTF("%d %d %.1f %.1f\r\n",g_tTimingOffInfo.HourTime,g_tTimingOffInfo.MinuteTime,g_tTimingOffInfo.RealTime,g_tTimingOffInfo.MemoryTime);
				DisplayTimingNum(g_AppSetRealTime);
			}
			SetTimedTimeActiveFlag(SH_SET);
			SetTimingModeState(true);			
			A2D_10_3_Set(true,Data2[0],Data1[1],Data2[1]);
			
			
		}
		else if(0 == Data1[0])
		{
			A2D_10_3_Set(false,0,0,0);
			ClearTimingState();
		}
//		BeepStart();
	}
    return;
}

static void P_10_19_RoomSelect_doSet(property_operation_t *o)
{
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_BOOLEAN)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }
	
    // TODO: 执行写操作: o->value.data.boolean.value;

	LOG_INFO("%s, %d\r\n",__func__,o->value.data.boolean.value);
	
    // 如果成功，返回代码: OPERATION_OK
    o->code = OPERATION_OK;
  
	A03_10_19_Set(o->value.data.boolean.value);
//	BeepStart();
    //上报状态，通知app状态变化，统一APP修改、定时器触发后的上报机制
//    P_10_19_RoomSelect_doChange_notify(o->value.data.boolean.value);

    return;
}

static void P_10_4_SleepDiy_doSet(property_operation_t *o)
{
	InDoorBusReg Reg = {0};
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_BOOLEAN)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

    // TODO: 执行写操作: o->value.data.boolean.value;

	LOG_INFO("%s, %d\r\n",__func__,o->value.data.boolean.value);
	
    // 如果成功，返回代码: OPERATION_OK
    o->code = OPERATION_OK;
		
	Reg.V8 = GetPbsRegValue(0x8E);			
	if(Reg.BIT.bit2) return;
	
	A06_10_4_Set(o->value.data.boolean.value);	

    //上报状态，通知app状态变化，统一APP修改、定时器触发后的上报机制
//    P_10_4_SleepDiy_doChange_notify(o->value.data.boolean.value);

    return;
}

static void P_10_20_LikeDiy_doSet(property_operation_t *o)
{
	InDoorBusReg Reg = {0};
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_BOOLEAN)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

    // TODO: 执行写操作: o->value.data.boolean.value;

	LOG_INFO("%s, %d\r\n",__func__,o->value.data.boolean.value);
	
    // 如果成功，返回代码: OPERATION_OK
    o->code = OPERATION_OK;
		
	Reg.V8 = GetPbsRegValue(0x8E);	
	if(Reg.BIT.bit2) return;	
			
		
	A06_10_20_Set(o->value.data.boolean.value);
		
    //上报状态，通知app状态变化，统一APP修改、定时器触发后的上报机制
//    P_10_20_LikeDiy_doChange_notify(o->value.data.boolean.value);

    return;
}

void S_10_Enhance_doSet(property_operation_t *o)
{
    switch (o->piid)
    {
        case PIID_10_16_RoomSize:
            P_10_16_RoomSize_doSet(o);
            break;

        case PIID_10_1_FanPercent:
            P_10_1_FanPercent_doSet(o);
            break;

        case PIID_10_18_MaxfanSelect:
            P_10_18_MaxfanSelect_doSet(o);
            break;

        case PIID_10_3_Timer:
            P_10_3_Timer_doSet(o);
            break;

        case PIID_10_19_RoomSelect:
            P_10_19_RoomSelect_doSet(o);
            break;

        case PIID_10_4_SleepDiy:
            P_10_4_SleepDiy_doSet(o);
            break;

        case PIID_10_20_LikeDiy:
            P_10_20_LikeDiy_doSet(o);
            break;

        default:
            o->code = OPERATION_ERROR_VALUE;
            break;
    }
}
